/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    vector<vector<int> > ans;
    vector<int> tmp;
    void run(TreeNode *root, int sum) {
        if (root==NULL) return;
        tmp.push_back(root->val);
        if (root->left==NULL && root->right==NULL && root->val==sum) {
            ans.push_back(tmp);
        } else {
            run(root->left, sum-root->val);
            run(root->right, sum-root->val);
        }
        tmp.pop_back();
    }
public:
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        ans.clear();
        tmp.clear();
        run(root, sum);
        return ans;
    }
};
